<?php

namespace app\service;

use app\exception\ErrnoPlus;
use app\model\AliyunProductModel;
use share\exception\ApiException;
use share\service\BaseModelService;
use share\service\cache\CommonCacheKeyService;
use share\service\RedisService;
use share\utils\Helper;

class AliyunProductService extends BaseModelService
{

    public static function getModel()
    {
        return new AliyunProductModel;
    }

    public static function getAllByCache(){
        
        $key = CommonCacheKeyService::getCacheKey(CommonCacheKeyService::CACHE_KEY_ALIYUN_PRODUCT);        
        $data = RedisService::instance(RedisService::CACHE_DB_BASE)->getJson($key);
        if(empty($data)){       
            $allData = self::getAllByFilter([]);
            if(!$allData){
                return null;
            }
            $data = Helper::listArrayColumn($allData,['id','aliyun_project_id','pk','name']);
            RedisService::instance(RedisService::CACHE_DB_BASE)->setJson($key,$data,86400);
        }
        return $data;
    }

    public static function delCache(){
        $key = CommonCacheKeyService::getCacheKey(CommonCacheKeyService::CACHE_KEY_ALIYUN_PRODUCT); 
        RedisService::instance(RedisService::CACHE_DB_BASE)->del($key);
    }

    public static function fastGetByPk($pk)
    {
        $data = self::getAllByCache();
        return Helper::filterOne($data,['pk' => $pk]);
    }

    /**
     * 编辑条目 新增或者修改
     */
    public static function edit($info)
    {
        $id = $info['id'];
        unset($info['id']);
        $aliyun_project_id = $info['aliyun_project_id'];
        $project = AliyunProjectService::getBase($aliyun_project_id);
        if (empty($project)) {
            throw new ApiException("选择的项目已经不存在", ErrnoPlus::COMMON_INVALID_INPUT);
        }
        if ($id == 0) {
            self::addAliyunProduct($info);
        } else {
            self::updateAliyunProduct($id, $info);
        }
        self::delCache();
        return $info;
    }

    /**
     * 新增条目
     */
    public static function addAliyunProduct($info)
    {
        self::add($info);
    }

    /**
     * 修改条目
     */
    public static function updateAliyunProduct($id, $info)
    {
        $origin  = self::getBase($id);
        if (empty($origin)) {
            throw new ApiException("origin data is not exists", ErrnoPlus::COMMON_INVALID_INPUT);
        }
        self::update($id, $info);
    }

    /**
     * 删除条目
     */
    public static function delById($id)
    {
        $origin = self::getBase($id);
        if (empty($origin)) {
            return;
        }
        $res = self::del($id);
        self::delCache();
        return $res;
    }

    /**
     * 页面查询条件
     */
    public static function buildCondition($filter)
    {
        $res = [];
        if (isset($filter['id']) && !empty($filter['id'])) {
            $res['id'] = $filter['id'];
        }
        if (isset($filter['name']) && !empty($filter['name'])) {
            $res['name'] = ['like', '%' . $filter['name'] . '%'];
        }
        if (isset($filter['pk']) && !empty($filter['pk'])) {
            $res['pk'] = ['like', '%' . $filter['pk'] . '%'];
        }
        if (isset($filter['ps']) && !empty($filter['ps'])) {
            $res['ps'] = ['like', '%' . $filter['ps'] . '%'];
        }
        if (isset($filter['description']) && !empty($filter['description'])) {
            $res['description'] = ['like', '%' . $filter['description'] . '%'];
        }
        if (isset($filter['aliyun_project_id']) && !empty($filter['aliyun_project_id'])) {
            $res['aliyun_project_id'] = $filter['aliyun_project_id'];
        }
        return $res;
    }
}
